// subpages/pages/cancelNFCBind/cancelNFCBind.js
//获取应用实例
var QR = require("../../utils/qrcode.js");

var util = require('../../utils/util.js')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        realName:"",
        loadding : 0,
        
        timestart: 0, //长按开始
    timeend: 0, //长按结束
    canvasHidden: false, //canvas显示
    imagePath: '', //图片路径
    content:'',
    Time:''
       
    },
    
    // 定位



   
    onLoad: function (options) {
        var that = this;
    setInterval(function(){
        that.setData({
            Time: util.formatTime(new Date())
        });    
    },1000);   


        
        let year="";
        let month="";
        let day="";
        var d=new Date();
        year=d.getFullYear();
        month=d.getMonth()+1;
        day=d.getDate();
        if (month < 10) month = "0" + month;
        if (day < 10) day = "0" + day;
         var time = year + "-" + month + "-" + day;  
       
        var color='#2E309D';
        var jiaoyanCode = '今日未打卡！';
       wx.request({
        url: app.dailyUrl +'/record/getRecord',
        method: 'POST',
        data: {
            userId: parseInt(app.globalData.userId),
            createTime: time
        },
        dataType: 'json',
        success: res => {
            console.log("test", res.data.data);
            if(res.data.data==null){
                color='red'
                 jiaoyanCode = '今日未打卡！';
                // if (options) {
                // jiaoyanCode = options.jiaoyanCode;
                // }
                console.log(jiaoyanCode+this.data.content);
                var size = this.setCanvasSize(); //动态设置画布大小 
                this.createQrCode(color,jiaoyanCode, "mycanvas", size.w, size.h);
            }else{
                if(res.data.data.code==0){
                    color='#4A9C5C';
                     jiaoyanCode = res.data.data.code+' '+res.data.data.createTime+' '+JSON.stringify(res.data.data.userInfo);
                    // if (options) {
                    // jiaoyanCode = options.jiaoyanCode;
                    // }
                    //jiaoyanCode = JSON.stringify(res.data.data.info);
                    console.log(jiaoyanCode+this.data.content);
                    var size = this.setCanvasSize(); //动态设置画布大小 
                    this.createQrCode(color,jiaoyanCode, "mycanvas", size.w, size.h);
                }else{
                    color='red'
                    jiaoyanCode = res.data.data.code+' '+res.data.data.createTime+' '+JSON.stringify(res.data.data.userInfo);
                    // if (options) {
                    // jiaoyanCode = options.jiaoyanCode;
                    // }
                    console.log(jiaoyanCode+this.data.content);
                    var size = this.setCanvasSize(); //动态设置画布大小 
                    this.createQrCode(color,jiaoyanCode, "mycanvas", size.w, size.h);
                }
            }
            
            
            }
           
         
       })
        
        wx.request({
            url: app.baseUrl + '/WeChat/AccountManagement/getUserByWeChatId',
            method: 'POST',
            data: {
              weChatId: app.globalData.wechatId
            },
            dataType: 'json',
            success: res => {
               console.log(res.data[0].realName)
               this.setData({
                realName:res.data[0].realName,
                department:res.data[0].department
               
               })
              
            }
          });
          
         
          
    },
    getTime:function(e){
        var that = this;
        var currentTime = util.formatTime(new Date());
      },
    setCanvasSize: function () {
        var size = {};
        try {
          var res = wx.getSystemInfoSync();
          var scale = 430 / 400; 
          //不同屏幕下canvas的适配比例；设计稿是750宽 686是因为样式wxss文件中设置的大小
          var width = res.windowWidth / scale;
          var height = width; //canvas画布为正方形
          size.w = width;
          size.h = height;
        } catch (e) {
          // Do something when catch error
          console.log("获取设备信息失败" + e);
        }
        return size;
      },
    

    createQrCode: function (color,url, canvasId, cavW, cavH) {
        //调用插件中的draw方法，绘制二维码图片
        QR.api.draw(color,url, canvasId, cavW, cavH);
        
       
        setTimeout(() => {
          this.canvasToTempImage();
        }, 1000);
      },
      canvasToTempImage: function () {
        var that = this;
        //把当前画布指定区域的内容导出生成指定大小的图片，并返回文件路径。
        wx.canvasToTempFilePath({
          canvasId: 'mycanvas',
          success: function (res) {
            console.log(res, "dadas")
            var tempFilePath = res.tempFilePath;
            console.log(tempFilePath);
            that.setData({
              imagePath: tempFilePath,
              // canvasHidden:true
            });
          },
          fail: function (res) {
            console.log(res);
          }
        });
      },
      goToEpidemicPunch:function(e){
        wx.navigateTo({
          url: '/pages/epidemicPunch/epidemicPunch',
        })
    },
   
})